\chapter{Random Numbers}
\adevs\ has two classes that work together to generate random numbers. These classes are the \classname{random\_seq} class and the \classname{rv} class. The \classname{random\_seq} class provides uniformly distributed random numbers to the \classname{rv} class. The \classname{rv} class transforms these uniform random numbers into a variety of random number distributions.

The \classname{random\_seq} class is the interface for a random number generator. Its derived classes produce uniformly distributed pseudo-random numbers. The underlying random number stream is accessed with two methods. The method \methodname{next\_long} returns a random number as an unsigned long. The method \methodname{next\_dlb} refines the \methodname{next\_long} method by reducing that random number to a double precision number in the interval $[0,1]$. The random number sequence is initialized with the \methodname{set\_seed} method, and the entire random number generator can be copied with the \methodname{copy} method. To summarize, the \classname{random\_seq} class has four virtual methods
\begin{verbatim}
void set_seed(unsigned long seed) 
double next_dbl() 
random_seq* copy() const 
unsigned long next_long() 
\end{verbatim}
that must be implemented by any derived class.

\adevs\ comes with two implementations of the \classname{random\_seq} class: the \classname{crand} class and the \classname{mtrand} class. The \classname{crand} class uses the rand function from the standard C library to implement the required methods. Its implementation is trivial. I've listed it below as an example of how to implement the \classname{random\_seq} interface.
\begin{verbatim}
class crand: public random_seq {
    public:
        /// Create a generator with the default seed
        crand(){}
        /// Create a generator with the given seed
        crand(unsigned long seed) { srand (seed); }
        /// Set the seed for the random number generator
        void set_seed(unsigned long seed) { srand (seed); }
        /// Get the next double uniformly distributed in [0, 1]
        double next_dbl() { return (double)rand()/(double)RAND_MAX; } 
        /// Copy the random number generator
        unsigned long next_long() { return (unsigned long)rand(); }

        random_seq* copy() const { return new crand (); }
        /// Destructor
        ~crand(){}
};
\end{verbatim}

The \classname{mtrand} class implements the Mersenne Twister random number generator\footnote{M. Matsumoto and T. Nishimura, ``Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator", ACM Transactions on Modeling and Computer Simulation, Vol. 8, No. 1, January 1998, pgs. 3-30.}. This code is based on their open source implementation of the Mersenne Twister. Aside from its potential advantages as a random number generator, the \classname{mtrand} class differs from the \classname{crand} class by its ability to make deep copies. Every instance of the \classname{mtrand} class has its own random number stream.

The \classname{rv} class uses the uniform random numbers provided by a \classname{random\_seq} object to produce several different random number distributions: triangular, uniform, normal, exponential, lognormal, Poisson, Weibull, binomial, and many others. Every instance of the \classname{rv} class is created with a \classname{random\_seq}. The default is an \classname{mtrand} object, but any type of \classname{random\_seq} object can be passed to the \classname{rv} constructor. The different random distributions are sampled by calling the appropriate method: \methodname{triangular} for a triangular distribution, \methodname{exponential} for an exponential distribution, \methodname{poisson} for a Poisson distribution, etc. Because \adevs\ is open source software, if a new distribution is needed then you can add a method that implements it to the \classname{rv} class (and, I hope, contribute the expansion to the \adevs\ project).
